package perl.lexical.classes;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PNumber extends TokenClass {

	private final Pattern numberPattern = Pattern.compile("^\\d$");
	
	@Override
	public Token generateToken(String lexem) {
		Pattern p = Pattern.compile("([1-9]\\d*|0)(\\.\\d+)?[^\\.]*");
		Matcher m = p.matcher(lexem);
		if (m.matches()) {
			
			int i = 0;

			while(numberPattern.matcher(lexem.substring(i,i+1)).matches() || lexem.substring(i,i+1).equals(".")){
				i++;
			}
			
			String result = lexem.substring(0,i);
			
			return new Token(className, result);
		}

		return null;
	}
	
	
	public PNumber() {
		className = TokenClassEnum.PNUMBER;
	}

}
